const Router = require("@koa/router");
const db = require("./db");

const router = new Router();

router.get("/", async (ctx, next) => {
  const collection = db.collection("friends");
  // 查询结果特殊 是一个游标类型 可以通过forEach遍历 或者toArray转成数组
  const res = await collection.find().toArray();
  console.log(res);
  ctx.body = res;
});

router.get("/list", async (ctx, next) => {
  ctx.body = "this is list";
});

router.get("/add", async (ctx, next) => {
  const collection = db.collection("friends");
  await collection.insertOne({
    name: "不吃香菜",
    age: 19,
    sex: 0,
  });
  const res = await collection.find().toArray();
  console.log(res);
  ctx.body = res;
});

module.exports = router;
